package dk.mirasola.systemtraining.bridgewidgets.client.ui.editor;

import com.google.gwt.core.client.GWT;
import com.google.gwt.editor.client.LeafValueEditor;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.RadioButton;
import com.google.gwt.user.client.ui.Widget;
import dk.mirasola.systemtraining.bridgewidgets.client.ui.RenderUtil;
import dk.mirasola.systemtraining.bridgewidgets.shared.model.distributionfiltertree.LogicOperator;

public class LogicOperatorEditor extends Composite implements LeafValueEditor<LogicOperator> {
    interface LogicOperatorEditorUIBinder extends UiBinder<Widget, LogicOperatorEditor> {
    }

    private static LogicOperatorEditorUIBinder uiBinder = GWT.create(LogicOperatorEditorUIBinder.class);

    @UiField
    RadioButton or;

    @UiField
    RadioButton and;

    public LogicOperatorEditor() {
        initWidget(uiBinder.createAndBindUi(this));
        or.setHTML(RenderUtil.renderLogicOperator(LogicOperator.OR));
        and.setHTML(RenderUtil.renderLogicOperator(LogicOperator.AND));
    }

    @Override
    public void setValue(LogicOperator value) {
        if (value == null) {
            throw new NullPointerException();
        }
        or.setValue(value == LogicOperator.OR);
        and.setValue(value == LogicOperator.AND);
    }

    @Override
    public LogicOperator getValue() {
        if (or.getValue()) {
            return LogicOperator.OR;
        } else {
            return LogicOperator.AND;
        }
    }
}
